package com.example.easeimpro.controller.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.AlertDialog;
import android.app.Application;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.View;

import com.example.easeimpro.R;
import com.example.easeimpro.model.bean.Model;
import com.example.easeimpro.model.bean.UserInfo;
import com.example.easeimpro.model.dao.UserAccountDao;
import com.example.easeimpro.model.db.UserAccountDB;
import com.example.easeimpro.utils.Permission;
import com.hyphenate.chat.EMClient;

import java.util.ArrayList;
import java.util.List;

public class HomeLoginActivity extends BaseActivity {
    private static final String TAG = "HomeLoginActivity";
    private final int mRequestCode=100;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_login);

        options.setAutoLogin(false);
        if (EMClient.getInstance().isLoggedInBefore()) {

            //查詢本地用戶數據庫 用戶是否已經登錄
            UserInfo account = new UserAccountDao(this).getAccountByHxId(EMClient.getInstance().getCurrentUser());
            Log.e(TAG, "onCreate: " + EMClient.getInstance().getCurrentUser());
            if (account != null) {
                if (account.getHxId() != null) {
                    Model.getInstance().LoginSuccess(account);
                    IntentNextActivity(MainActivity.class);
                    finish();
                }
            }
        }
        findViewById(R.id.loginBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentNextActivity(LoginActivity.class);
            }
        });
        findViewById(R.id.RegisterBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转注册
                IntentNextActivity(RegisterActivity.class);
            }
        });
        new Permission().checkPermission(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == Permission.REQUEST_CODE){
            for (int i = 0; i < grantResults.length; i++) {
                if(grantResults[i] != PackageManager.PERMISSION_GRANTED){
                    Log.e(TAG, "onRequestPermissionsResult: "+permissions[i]+"not" );
                    Log.e(TAG, "onRequestPermissionsResult: "+grantResults[i]+"not" );
                }else{
                    Log.e(TAG, "onRequestPermissionsResult: "+permissions[i] );
                    Log.e(TAG, "onRequestPermissionsResult: "+grantResults[i] );
                }
            }
        }
    }
}